﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Xml.Linq;
using xycn.Base;
using xycn.IDbService;

namespace xycn.DbService
{
    /// <summary>
    /// 数据操作实现
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class SugarService : IDbService.IDbService
    {
        #region 数据库初始化
        /// <summary>
        /// 数据库对象
        /// </summary>
        public SqlSugarClient _sqlclient = null;
        private string connstr;
        private string dbtype;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="connstr"></param>
        /// <param name="dbtype"></param>
        public SugarService(string connstr, string dbtype)
        {
            this.connstr = connstr;
            this.dbtype = dbtype;
        }

        /// <summary>
        /// 基础操作
        /// </summary>
        public SqlSugarClient _baseDb
        {
            get
            {
                if (_sqlclient == null)
                {
                    _sqlclient = GetInstance();
                }
                return _sqlclient;
            }
        }
        /// <summary>
        /// 数据库类型
        /// </summary>
        private SqlSugar.DbType DataBaseType
        {
            get
            {
                switch (dbtype.ToLower())
                {
                    case "sqlserver": return SqlSugar.DbType.SqlServer;
                    case "sqlite": return SqlSugar.DbType.Sqlite;
                    case "oracle": return SqlSugar.DbType.Oracle;
                    case "postgresql": return SqlSugar.DbType.PostgreSQL;
                    case "达梦": return SqlSugar.DbType.Dm;
                    case "人大金仓": return SqlSugar.DbType.Kdbndp;
                }
                return SqlSugar.DbType.MySql;
            }
        }

        private SqlSugarClient GetInstance()
        {
            //创建数据库对象
            SqlSugarClient db = new SqlSugarClient(new ConnectionConfig
            {
                ConnectionString = connstr,
                DbType = DataBaseType,
                InitKeyType = InitKeyType.Attribute,
                IsAutoCloseConnection = true
            });

            //添加Sql打印事件，开发中可以删掉这个代码
            db.Aop.OnLogExecuting = (sql, pars) =>
            {
                foreach (var param in pars) sql = sql.Replace(param?.ParameterName, $"'{param.Value?.ToString()}'");
                Console.WriteLine(sql);
                //Log4Helper.Info("ExecSql:\r\n" + sql);
            };
            return db;
        }

        #endregion

        #region 私有方法
        /// <summary>
        /// 构建Sql参数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dicwhere"></param>
        /// <param name="lstparams"></param>
        /// <returns></returns>
        public string BuilderSql<T>(Dictionary<string, object> dicwhere, out List<SugarParameter> lstparams) where T : new()
        {
            lstparams = new List<SugarParameter>();

            if (dicwhere != null && dicwhere.Count() > 0)
            {
                List<object> sqlvalues;

                //得到 a=? and b=?
                var sqlwhere = SqlHelper.GetSqlByDic<T>(dicwhere, out sqlvalues);

                string[] temp = sqlwhere.Split('?');
                for (int i = 0; i < sqlvalues.Count; i++)
                {
                    temp[i] = temp[i] + "@p" + (i + 1).ToString();

                    lstparams.Add(new SugarParameter($"@p{(i + 1)}", sqlvalues[i], System.Data.DbType.String));
                }

                sqlwhere = string.Join("", temp);

                return sqlwhere;
            }
            return "";
        }
        #endregion

        #region 添加数据
        /// <summary>
        /// 添加一条数据,并返回添加成功后的主键ID
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual Result<string> AddItem<T>(T entity) where T : class, new()
        {
            var res = _baseDb.Insertable(entity).IgnoreColumns(true).ExecuteCommand();

            return new Result<string>() { Data = res.ToString(), IsSucceed = res > 0 };
        }

        /// <summary>
        /// 添加多条数据 返回影响行数
        /// </summary>
        /// <param name="entitys"></param>
        /// <returns></returns>
        public virtual Result<int> AddItems<T>(List<T> entitys) where T : class, new()
        {
            var res = _baseDb.Insertable(entitys).ExecuteCommand();

            return new Result<int>() { Data = res, IsSucceed = res > 0 };
        }

        #endregion

        #region 删除

        /// <summary>
        /// 删除数据 返回影响行数
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public virtual Result<int> DeleteItem<T>(Expression<Func<T, bool>> expression) where T : class, new()
        {
            var res = _baseDb.Deleteable(expression).ExecuteCommand();

            return new Result<int>() { Data = res, IsSucceed = res > 0 };
        }

        /// <summary>
        /// 删除数据 返回影响行数
        /// </summary>
        /// <param name="queryWhere"></param>
        /// <returns></returns>
        public virtual Result<int> DeleteItemByDic<T>(Dictionary<string, object> queryWhere) where T : class, new()
        {
            var tableName = typeof(T).Name;

            var sql = "delete FROM " + tableName + " where ";

            List<SugarParameter> sugarParam;
            var sqlwhere = BuilderSql<T>(queryWhere, out sugarParam);
            if (sqlwhere.Length > 0)
            {
                sql += sqlwhere;
            }
            else
            {
                return new Result<int>() { Data = -1 };
            }
            var res = _baseDb.Ado.ExecuteCommand(sql, sugarParam);
            return new Result<int>() { Data = res, IsSucceed = res > 0 };
        }

        #endregion

        #region 查询
        /// <summary>
        /// 查询所有数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="expression"></param>
        /// <param name="orderby"></param>
        /// <returns></returns>
        public virtual Result<List<T>> GetList<T>(Expression<Func<T, bool>> expression, string orderby = "") where T : class, new()
        {
            try
            {
                var res = new List<T>();
                if (expression == null)
                {
                    if (string.IsNullOrWhiteSpace(orderby))
                    {
                        res = _baseDb.Queryable<T>().ToList();
                    }
                    else
                    {
                        res = _baseDb.Queryable<T>().OrderBy(orderby).ToList();
                    }
                }
                else
                {
                    if (string.IsNullOrWhiteSpace(orderby))
                    {
                        res = _baseDb.Queryable<T>().Where(expression).ToList();
                    }
                    else
                    {
                        res = _baseDb.Queryable<T>().Where(expression).OrderBy(orderby).ToList();
                    }
                }
                return new Result<List<T>>() { IsSucceed = res != null, Data = res };
            }
            catch (Exception ex)
            {
                return new Result<List<T>>() { ErrorCode = 500, Message = ex.Message };
            }
        }

        /// <summary>
        /// 查询所有数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="queryWhere">key为  orderby的时候 自动转换为排序字段 格式 id asc/id desc</param>
        /// <returns></returns>
        public virtual List<T> GetListByDic<T>(Dictionary<string, object> queryWhere) where T : class, new()
        {
            var tableName = typeof(T).Name;

            var sql = "select * from " + tableName;

            List<SugarParameter> sugarParam;

            var orderby = " ";
            if (queryWhere.ContainsKey("orderby"))
            {
                orderby = " order by " + queryWhere["orderby"].ToString() + " ";
                queryWhere.Remove("orderby");
            }

            var sqlwhere = BuilderSql<T>(queryWhere, out sugarParam);
            if (sqlwhere.Length > 0)
            {
                sql += " where " + sqlwhere;
            }
            //if (!sql.ToLower().Contains("isdeleted"))
            //{
            //    sql += $" {(sql.ToLower().Contains("where") ? " and " : " where ")} IsDeleted=0 ";
            //}

            sql += orderby;

            return _baseDb.Ado.SqlQuery<T>(sql, sugarParam);
        }

        /// <summary>
        /// 分页查询数据
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="totalNum"></param>
        /// <param name="orderby"></param>
        /// <returns></returns>
        public virtual Result<List<T>> GetListByPage<T>(Expression<Func<T, bool>> expression, int page, int limit, ref int totalNum, string orderby = "") where T : class, new()
        {
            var data = new List<T>();

            if (string.IsNullOrWhiteSpace(orderby))
            {
                data = _baseDb.Queryable<T>().Where(expression).ToPageList(page, limit, ref totalNum);
            }
            else
            {
                data = _baseDb.Queryable<T>().Where(expression).OrderBy(orderby).ToPageList(page, limit, ref totalNum);
            }

            return new Result<List<T>>()
            {
                Data = data,
                IsSucceed = true
            };
        }

        /// <summary>
        /// 获取一条数据
        /// </summary>
        /// <returns></returns>
        public virtual Result<int> GetCount<T>(Expression<Func<T, bool>> expression = null) where T : class, new()
        {
            try
            {
                var res = 0;
                if (expression == null)
                {
                    res = _baseDb.Queryable<T>().Count();
                }
                else
                {
                    res = _baseDb.Queryable<T>().Count(expression);
                }

                return new Result<int>() { Data = res };
            }
            catch (Exception ex)
            {
                return new Result<int>() { ErrorCode = 500, Message = ex.Message };
            }
        }

        /// <summary>
        /// 获取一条数据
        /// </summary>
        /// <returns></returns>
        public virtual Result<T> GetOne<T>(Expression<Func<T, bool>> expression) where T : class, new()
        {
            try
            {
                var res = _baseDb.Queryable<T>().First(expression);

                return new Result<T>() { Data = res, IsSucceed = res != null };
            }
            catch (Exception ex)
            {
                return new Result<T>() { ErrorCode = 500, Message = ex.Message };
            }
        }

        #endregion

        #region 修改数据

        /// <summary>
        /// 修改多列数据 返回影响行数
        /// </summary>
        /// <param name="upfields"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        public virtual Result<int> UpdateFields<T>(Expression<Func<T, T>> upfields, Expression<Func<T, bool>> where) where T : class, new()
        {
            var res = _baseDb.Updateable<T>().SetColumns(upfields)
                .Where(where)
                .ExecuteCommand();

            return new Result<int>() { Data = res, IsSucceed = res > 0 };
        }

        /// <summary>
        /// 修改多列数据 返回影响行数
        /// </summary>
        /// <param name="upfields"></param>
        /// <param name="where"></param>
        /// <param name="ignoreColumns">忽略的列 it => new { it.CreateTime,it.TestId }</param>
        /// <returns></returns>
        public virtual Result<int> UpdateFieldsIgnore<T>(Expression<Func<T, T>> upfields, Expression<Func<T, bool>> where) where T : class, new()
        {
            var res = _baseDb.Updateable<T>().SetColumns(upfields)
               .Where(where)
               .ExecuteCommand();

            //var result = _baseDb.Updateable<Members>()
            //            .SetColumns(it => new Members() { WxNickName = "12312", GroupId = 1231 })
            //            .IgnoreColumns(it => new { it.GroupId })
            //            .Where(it => it.KID == 1)
            //            .ExecuteCommand();

            return new Result<int>() { Data = res, IsSucceed = res > 0 };
        }

        /// <summary>
        /// 修改多列数据 返回影响行数
        /// </summary>
        /// <param name="upfields"></param>
        /// <param name="where"></param>
        /// <param name="onlyColumns">只修改的列 it => new { it.CreateTime,it.TestId }</param>
        /// <returns></returns>
        public virtual Result<int> UpdateFieldsOnly<T>(Expression<Func<T, T>> upfields, Expression<Func<T, bool>> where, Expression<Func<T, object>> onlyColumns) where T : class, new()
        {
            var res = _baseDb.Updateable<T>().SetColumns(upfields)
                .UpdateColumns(onlyColumns)
                .Where(where)
                .ExecuteCommand();

            return new Result<int>() { Data = res, IsSucceed = res > 0 };
        }


        /// <summary>
        /// 修改一列数据 返回影响行数
        /// </summary>
        /// <param name="columns"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        public virtual Result<int> UpdateOneField<T>(Expression<Func<T, bool>> columns, Expression<Func<T, bool>> where) where T : class, new()
        {
            var res = _baseDb.Updateable<T>().SetColumns(columns)
                .Where(where)
                .ExecuteCommand();

            return new Result<int>() { Data = res, IsSucceed = res > 0 };
        }

        #endregion

        #region 原始Sql支持

        /// <summary>
        /// 数据查询GetDataTable("select * from `order` where @id>0  or name=@name", new { id = 1, name = "2" });
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <param name="parameters">参数  new { id = 1, name = "2" }</param>
        /// <returns></returns>
        public virtual DataTable GetDataTable(string sql, object parameters)
        {
            var res = _baseDb.Ado.GetDataTable(sql, parameters);
            return res;
        }

        /// <summary>
        /// 数据查询 GetDataSetAll("select * from `order` where @id>0  or name=@name", new { id = 1, name = "2" });
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <param name="parameters">参数  new { id = 1, name = "2" }</param>
        /// <returns></returns>
        public virtual DataSet GetDataSetAll(string sql, object parameters)
        {
            return _baseDb.Ado.GetDataSetAll(sql, parameters ?? new { });
        }

        /// <summary>
        /// 数据查询
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public virtual object GetScalar(string sql, object parameters)
        {
            return _baseDb.Ado.GetScalar(sql, parameters ?? new { });
        }

        /// <summary>
        /// 数据查询
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public virtual string GetString(string sql, object parameters)
        {
            return _baseDb.Ado.GetString(sql, parameters ?? new { });
        }

        /// <summary>
        /// 执行Sql
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public virtual int ExecuteCommand(string sql, object parameters)
        {
            return _baseDb.Ado.ExecuteCommand(sql, parameters ?? new { });
        }

        /// <summary>
        /// 根据Sql查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public virtual List<T> SqlQuery<T>(string sql, object parameters = null) where T : class, new()
        {
            return _baseDb.Ado.SqlQuery<T>(sql, parameters ?? new { });
        }

        #endregion

        #region 事务
        /// <summary>
        /// 开始事务
        /// </summary>
        public virtual void BeginTran()
        {
            _baseDb.BeginTran();
        }
        /// <summary>
        /// 回滚事务
        /// </summary>
        public virtual void RollbackTran()
        {
            _baseDb.RollbackTran();
        }
        /// <summary>
        /// 结束事务
        /// </summary>
        public virtual void CommitTran()
        {
            _baseDb.CommitTran();
        }
        #endregion

        #region 关联查询
        /// <summary>
        /// 分组获取数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="where"></param>
        /// <param name="groupby"></param>
        /// <returns></returns>
        public virtual Result<List<T>> GroupBy<T>(Expression<Func<T, bool>> where, Expression<Func<T, object>> groupby) where T : class, new()
        {
            try
            {
                var res = _baseDb.Queryable<T>().Where(where).GroupBy(groupby).ToList();

                return new Result<List<T>>() { Data = res, IsSucceed = res != null };
            }
            catch (Exception ex)
            {
                return new Result<List<T>>() { ErrorCode = 500, Message = ex.Message };
            }
        }


        /// <summary>
        /// 2张表 关联查询
        /// </summary>
        /// <typeparam name="T">第一张表</typeparam>
        /// <typeparam name="T2">第二张表</typeparam>
        /// <typeparam name="T3">返回的数据视图</typeparam>
        /// <param name="joinExpression">关联名</param>
        /// <param name="sqlJoinWhere">格式 a.kid=b.kid and 1=1 </param>
        /// <param name="joinTypes">枚举类型</param>
        /// <param name="where">where条件 Lambda 表达式</param>
        /// <param name="orderby">排序字段 Lambda 表达式</param>
        /// <param name="orderType">排序类型 0ASC 1DESC</param>
        /// <param name="rowCount">总数</param>
        /// <param name="resFields">返回字段列表 a.*,b.*,a.kid as aakid </param>
        /// <param name="page">分页 默认1</param>
        /// <param name="limit">每页条数 默认20 </param>
        public virtual Result<List<T3>> GetList<T, T2, T3>(Expression<Func<T, T2, bool>> joinExpression, string sqlJoinWhere, IDbService.JoinType joinTypes, Expression<Func<T, bool>> where
            , Expression<Func<T, object>> orderby, OrderType orderType, ref int rowCount, string resFields = "", int page = 1, int limit = 20) where T : class, new()
        {
            try
            {
                var sugarJoinType = SqlSugar.JoinType.Left;
                if (joinTypes == IDbService.JoinType.Left) sugarJoinType = SqlSugar.JoinType.Left;
                if (joinTypes == IDbService.JoinType.Right) sugarJoinType = SqlSugar.JoinType.Right;
                if (joinTypes == IDbService.JoinType.Inner) sugarJoinType = SqlSugar.JoinType.Inner;

                var res = new List<T3>();
                if (string.IsNullOrWhiteSpace(resFields))
                {
                    for (int i = 0; i < joinExpression.Parameters.Count(); i++)
                    {
                        resFields += joinExpression.Parameters[i].Name + ".*,";
                    }
                    resFields = resFields.Trim(',');
                }
                if (where == null)
                {
                    //res = _baseDb.Queryable<T>(joinExpression.Parameters[0].Name).AS(typeof(T).Name)
                    //    .AddJoinInfo(typeof(T2).Name, joinExpression.Parameters[1].Name, sqlJoinWhere, sugarJoinType)
                    //    .OrderBy(orderby, orderType == 0 ? OrderByType.Asc : OrderByType.Desc)
                    //    .Select<T3>().ToPageList(page, limit).ToList();

                    res = _baseDb.Queryable<T>(joinExpression.Parameters[0].Name).AS(typeof(T).Name)
                        .AddJoinInfo(typeof(T2).Name, joinExpression.Parameters[1].Name, sqlJoinWhere, sugarJoinType)
                        .OrderBy(orderby, orderType == 0 ? OrderByType.Asc : OrderByType.Desc)
                        //.Select<T3>().ToPageList(page, limit).ToList();
                        //.Select<T3>(resView).ToPageList(page, limit).ToList();
                        .Select<T3>(resFields).ToPageList(page, limit, ref rowCount).ToList();

                    //_baseDb.Queryable<Members, Members2, MemberView>((a, b, c) => new SqlSugar.JoinQueryInfos(
                    //      SqlSugar.JoinType.Left, a.KID == b.KID2
                    //      ))
                    //    .Select((a, b, c) => new
                    //    {
                    //        xx = a.KID,
                    //        xx3=b.KID2
                    //    }).ToList();
                }
                else
                {
                    res = _baseDb.Queryable<T>(joinExpression.Parameters[0].Name).AS(typeof(T).Name)
                      .AddJoinInfo(typeof(T2).Name, joinExpression.Parameters[1].Name, sqlJoinWhere, sugarJoinType)
                      .Where(where)
                      .OrderBy(orderby, orderType == 0 ? OrderByType.Asc : OrderByType.Desc)
                       //.Select<T3>().ToPageList(page, limit).ToList();
                       //.Select<T3>(resView).ToPageList(page, limit).ToList();
                       .Select<T3>(resFields).ToPageList(page, limit, ref rowCount).ToList();
                }
                return new Result<List<T3>>() { IsSucceed = res != null, Data = res };
            }
            catch (Exception ex)
            {
                return new Result<List<T3>>() { ErrorCode = 500, Message = ex.Message };
            }
        }

        /// <summary>
        /// 3张表 关联查询
        /// </summary>
        /// <typeparam name="T">第一张表</typeparam>
        /// <typeparam name="T2">第二张表</typeparam>
        /// <typeparam name="T3">第三张表</typeparam>
        /// <typeparam name="T4">返回的数据视图</typeparam>
        /// <param name="joinExpression">关联名</param>
        /// <param name="sqlJoinWhere">格式 a.kid=b.kid and 1=1 </param>
        /// <param name="joinTypes">枚举类型</param>
        /// <param name="where">where条件 Lambda 表达式</param>
        /// <param name="orderby">排序字段 Lambda 表达式</param>
        /// <param name="orderType">排序类型 0ASC 1DESC</param>
        /// <param name="rowCount">总数</param>
        /// <param name="resFields">返回字段列表 a.*,b.*,a.kid as aakid </param>
        /// <param name="page">分页 默认1</param>
        /// <param name="limit">每页条数 默认20 </param>
        /// <returns></returns>
        public virtual Result<List<T4>> GetList<T, T2, T3, T4>(Expression<Func<T, T2, T3, bool>> joinExpression, List<string> sqlJoinWhere, List<IDbService.JoinType> joinTypes, Expression<Func<T, bool>> where
            , Expression<Func<T, object>> orderby, OrderType orderType, ref int rowCount, string resFields = "", int page = 1, int limit = 20) where T : class, new()
        {
            try
            {
                List<SqlSugar.JoinType> SqlJoinType = new List<SqlSugar.JoinType>();
                joinTypes.ForEach((item) =>
                {
                    if (item == IDbService.JoinType.Left) SqlJoinType.Add(SqlSugar.JoinType.Left);
                    if (item == IDbService.JoinType.Right) SqlJoinType.Add(SqlSugar.JoinType.Right);
                    if (item == IDbService.JoinType.Inner) SqlJoinType.Add(SqlSugar.JoinType.Inner);
                });

                var res = new List<T4>();
                if (string.IsNullOrWhiteSpace(resFields))
                {
                    for (int i = 0; i < joinExpression.Parameters.Count(); i++)
                    {
                        resFields += joinExpression.Parameters[i].Name + ".*,";
                    }
                    resFields = resFields.Trim(',');
                }
                if (where == null)
                {
                    res = _baseDb.Queryable<T>(joinExpression.Parameters[0].Name).AS(typeof(T).Name)
                        .AddJoinInfo(typeof(T2).Name, joinExpression.Parameters[1].Name, sqlJoinWhere[0], SqlJoinType[0])
                        .AddJoinInfo(typeof(T3).Name, joinExpression.Parameters[2].Name, sqlJoinWhere[1], SqlJoinType[1])
                        .OrderBy(orderby, orderType == 0 ? OrderByType.Asc : OrderByType.Desc)
                        .Select<T4>(resFields).ToPageList(page, limit, ref rowCount).ToList();
                }
                else
                {
                    res = _baseDb.Queryable<T>(joinExpression.Parameters[0].Name).AS(typeof(T).Name)
                        .AddJoinInfo(typeof(T2).Name, joinExpression.Parameters[1].Name, sqlJoinWhere[0], SqlJoinType[0])
                        .AddJoinInfo(typeof(T3).Name, joinExpression.Parameters[2].Name, sqlJoinWhere[1], SqlJoinType[1])
                        .Where(where)
                        .OrderBy(orderby, orderType == 0 ? OrderByType.Asc : OrderByType.Desc)
                        .Select<T4>(resFields).ToPageList(page, limit, ref rowCount).ToList();
                }
                return new Result<List<T4>>() { IsSucceed = res != null, Data = res };
            }
            catch (Exception ex)
            {
                return new Result<List<T4>>() { ErrorCode = 500, Message = ex.Message };
            }
        }

        /// <summary>
        /// 4张表 关联查询
        /// </summary>
        /// <typeparam name="T">第一张表</typeparam>
        /// <typeparam name="T2">第二张表</typeparam>
        /// <typeparam name="T3">第三张表</typeparam>
        /// <typeparam name="T4">第四张表</typeparam>
        /// <typeparam name="T5">返回的数据视图</typeparam>
        /// <param name="joinExpression">关联名</param>
        /// <param name="sqlJoinWhere">格式 a.kid=b.kid and 1=1 </param>
        /// <param name="joinTypes">枚举类型</param>
        /// <param name="where">where条件 Lambda 表达式</param>
        /// <param name="orderby">排序字段 Lambda 表达式</param>
        /// <param name="orderType">排序类型 0ASC 1DESC</param>
        /// <param name="rowCount">总数</param>
        /// <param name="resFields">返回字段列表 a.*,b.*,a.kid as aakid </param>
        /// <param name="page">分页 默认1</param>
        /// <param name="limit">每页条数 默认20 </param>
        /// <returns></returns>
        public virtual Result<List<T5>> GetList<T, T2, T3, T4, T5>(Expression<Func<T, T2, T3, T4, bool>> joinExpression, List<string> sqlJoinWhere, List<IDbService.JoinType> joinTypes, Expression<Func<T, bool>> where
            , Expression<Func<T, object>> orderby, OrderType orderType, ref int rowCount, string resFields = "", int page = 1, int limit = 20) where T : class, new()
        {
            try
            {
                List<SqlSugar.JoinType> SqlJoinType = new List<SqlSugar.JoinType>();
                joinTypes.ForEach((item) =>
                {
                    if (item == IDbService.JoinType.Left) SqlJoinType.Add(SqlSugar.JoinType.Left);
                    if (item == IDbService.JoinType.Right) SqlJoinType.Add(SqlSugar.JoinType.Right);
                    if (item == IDbService.JoinType.Inner) SqlJoinType.Add(SqlSugar.JoinType.Inner);
                });

                var res = new List<T5>();
                if (string.IsNullOrWhiteSpace(resFields))
                {
                    for (int i = 0; i < joinExpression.Parameters.Count(); i++)
                    {
                        resFields += joinExpression.Parameters[i].Name + ".*,";
                    }
                    resFields = resFields.Trim(',');
                }
                if (where == null)
                {
                    res = _baseDb.Queryable<T>(joinExpression.Parameters[0].Name).AS(typeof(T).Name)
                        .AddJoinInfo(typeof(T2).Name, joinExpression.Parameters[1].Name, sqlJoinWhere[0], SqlJoinType[0])
                        .AddJoinInfo(typeof(T3).Name, joinExpression.Parameters[2].Name, sqlJoinWhere[1], SqlJoinType[1])
                        .AddJoinInfo(typeof(T4).Name, joinExpression.Parameters[3].Name, sqlJoinWhere[2], SqlJoinType[2])
                        .OrderBy(orderby, orderType == 0 ? OrderByType.Asc : OrderByType.Desc)
                        .Select<T5>(resFields).ToPageList(page, limit, ref rowCount).ToList();
                }
                else
                {
                    res = _baseDb.Queryable<T>(joinExpression.Parameters[0].Name).AS(typeof(T).Name)
                        .AddJoinInfo(typeof(T2).Name, joinExpression.Parameters[1].Name, sqlJoinWhere[0], SqlJoinType[0])
                        .AddJoinInfo(typeof(T3).Name, joinExpression.Parameters[2].Name, sqlJoinWhere[1], SqlJoinType[1])
                        .AddJoinInfo(typeof(T4).Name, joinExpression.Parameters[3].Name, sqlJoinWhere[2], SqlJoinType[2])
                        .Where(where)
                        .OrderBy(orderby, orderType == 0 ? OrderByType.Asc : OrderByType.Desc)
                        .Select<T5>(resFields).ToPageList(page, limit, ref rowCount).ToList();
                }
                return new Result<List<T5>>() { IsSucceed = res != null, Data = res };
            }
            catch (Exception ex)
            {
                return new Result<List<T5>>() { ErrorCode = 500, Message = ex.Message };
            }
        }


        /// <summary>
        /// 5张表 关联查询
        /// </summary>
        /// <typeparam name="T">第一张表</typeparam>
        /// <typeparam name="T2">第二张表</typeparam>
        /// <typeparam name="T3">第三张表</typeparam>
        /// <typeparam name="T4">第四张表</typeparam>
        /// <typeparam name="T5">第五张表</typeparam>
        /// <typeparam name="T6">返回的数据视图</typeparam>
        /// <param name="joinExpression">关联名</param>
        /// <param name="sqlJoinWhere">格式 a.kid=b.kid and 1=1 </param>
        /// <param name="joinTypes">枚举类型</param>
        /// <param name="where">where条件 Lambda 表达式</param>
        /// <param name="orderby">排序字段 Lambda 表达式</param>
        /// <param name="orderType">排序类型 0ASC 1DESC</param>
        /// <param name="rowCount">总数</param>
        /// <param name="resFields">返回字段列表 a.*,b.*,a.kid as aakid </param>
        /// <param name="page">分页 默认1</param>
        /// <param name="limit">每页条数 默认20 </param>
        /// <returns></returns>
        public virtual Result<List<T6>> GetList<T, T2, T3, T4, T5, T6>(Expression<Func<T, T2, T3, T4, T5, bool>> joinExpression, List<string> sqlJoinWhere, List<IDbService.JoinType> joinTypes, Expression<Func<T, bool>> where
            , Expression<Func<T, object>> orderby, OrderType orderType, ref int rowCount, string resFields = "", int page = 1, int limit = 20) where T : class, new()
        {
            try
            {
                List<SqlSugar.JoinType> SqlJoinType = new List<SqlSugar.JoinType>();
                joinTypes.ForEach((item) =>
                {
                    if (item == IDbService.JoinType.Left) SqlJoinType.Add(SqlSugar.JoinType.Left);
                    if (item == IDbService.JoinType.Right) SqlJoinType.Add(SqlSugar.JoinType.Right);
                    if (item == IDbService.JoinType.Inner) SqlJoinType.Add(SqlSugar.JoinType.Inner);
                });

                var res = new List<T6>();
                if (string.IsNullOrWhiteSpace(resFields))
                {
                    for (int i = 0; i < joinExpression.Parameters.Count(); i++)
                    {
                        resFields += joinExpression.Parameters[i].Name + ".*,";
                    }
                    resFields = resFields.Trim(',');
                }
                if (where == null)
                {
                    res = _baseDb.Queryable<T>(joinExpression.Parameters[0].Name).AS(typeof(T).Name)
                        .AddJoinInfo(typeof(T2).Name, joinExpression.Parameters[1].Name, sqlJoinWhere[0], SqlJoinType[0])
                        .AddJoinInfo(typeof(T3).Name, joinExpression.Parameters[2].Name, sqlJoinWhere[1], SqlJoinType[1])
                        .AddJoinInfo(typeof(T4).Name, joinExpression.Parameters[3].Name, sqlJoinWhere[2], SqlJoinType[2])
                        .AddJoinInfo(typeof(T5).Name, joinExpression.Parameters[4].Name, sqlJoinWhere[3], SqlJoinType[3])
                        .OrderBy(orderby, orderType == 0 ? OrderByType.Asc : OrderByType.Desc)
                        .Select<T6>(resFields).ToPageList(page, limit, ref rowCount).ToList();
                }
                else
                {
                    res = _baseDb.Queryable<T>(joinExpression.Parameters[0].Name).AS(typeof(T).Name)
                        .AddJoinInfo(typeof(T2).Name, joinExpression.Parameters[1].Name, sqlJoinWhere[0], SqlJoinType[0])
                        .AddJoinInfo(typeof(T3).Name, joinExpression.Parameters[2].Name, sqlJoinWhere[1], SqlJoinType[1])
                        .AddJoinInfo(typeof(T4).Name, joinExpression.Parameters[3].Name, sqlJoinWhere[2], SqlJoinType[2])
                        .AddJoinInfo(typeof(T5).Name, joinExpression.Parameters[4].Name, sqlJoinWhere[3], SqlJoinType[3])
                        .Where(where)
                        .OrderBy(orderby, orderType == 0 ? OrderByType.Asc : OrderByType.Desc)
                        .Select<T6>(resFields).ToPageList(page, limit, ref rowCount).ToList();
                }
                return new Result<List<T6>>() { IsSucceed = res != null, Data = res };
            }
            catch (Exception ex)
            {
                return new Result<List<T6>>() { ErrorCode = 500, Message = ex.Message };
            }
        }

        /// <summary>
        /// 6张表 关联查询
        /// </summary>
        /// <typeparam name="T">第一张表</typeparam>
        /// <typeparam name="T2">第二张表</typeparam>
        /// <typeparam name="T3">第三张表</typeparam>
        /// <typeparam name="T4">第四张表</typeparam>
        /// <typeparam name="T5">第五张表</typeparam>
        /// <typeparam name="T6">第六张表</typeparam>
        /// <typeparam name="T7">返回的数据视图</typeparam>
        /// <param name="joinExpression">关联名</param>
        /// <param name="sqlJoinWhere">格式 a.kid=b.kid and 1=1 </param>
        /// <param name="joinTypes">枚举类型</param>
        /// <param name="where">where条件 Lambda 表达式</param>
        /// <param name="orderby">排序字段 Lambda 表达式</param>
        /// <param name="orderType">排序类型 0ASC 1DESC</param>
        /// <param name="rowCount">数据条数</param>
        /// <param name="resFields">返回字段列表 a.*,b.*,a.kid as aakid </param>
        /// <param name="page">分页 默认1</param>
        /// <param name="limit">每页条数 默认20 </param>
        /// <returns></returns>
        public virtual Result<List<T7>> GetList<T, T2, T3, T4, T5, T6, T7>(Expression<Func<T, T2, T3, T4, T5, T6, bool>> joinExpression, List<string> sqlJoinWhere, List<IDbService.JoinType> joinTypes, Expression<Func<T, bool>> where
            , Expression<Func<T, object>> orderby, OrderType orderType, ref int rowCount, string resFields = "", int page = 1, int limit = 20) where T : class, new()
        {
            try
            {
                List<SqlSugar.JoinType> SqlJoinType = new List<SqlSugar.JoinType>();
                joinTypes.ForEach((item) =>
                {
                    if (item == IDbService.JoinType.Left) SqlJoinType.Add(SqlSugar.JoinType.Left);
                    if (item == IDbService.JoinType.Right) SqlJoinType.Add(SqlSugar.JoinType.Right);
                    if (item == IDbService.JoinType.Inner) SqlJoinType.Add(SqlSugar.JoinType.Inner);
                });

                var res = new List<T7>();
                if (string.IsNullOrWhiteSpace(resFields))
                {
                    for (int i = 0; i < joinExpression.Parameters.Count(); i++)
                    {
                        resFields += joinExpression.Parameters[i].Name + ".*,";
                    }
                    resFields = resFields.Trim(',');
                }
                if (where == null)
                {
                    res = _baseDb.Queryable<T>(joinExpression.Parameters[0].Name).AS(typeof(T).Name)
                        .AddJoinInfo(typeof(T2).Name, joinExpression.Parameters[1].Name, sqlJoinWhere[0], SqlJoinType[0])
                        .AddJoinInfo(typeof(T3).Name, joinExpression.Parameters[2].Name, sqlJoinWhere[1], SqlJoinType[1])
                        .AddJoinInfo(typeof(T4).Name, joinExpression.Parameters[3].Name, sqlJoinWhere[2], SqlJoinType[2])
                        .AddJoinInfo(typeof(T5).Name, joinExpression.Parameters[4].Name, sqlJoinWhere[3], SqlJoinType[3])
                        .AddJoinInfo(typeof(T6).Name, joinExpression.Parameters[5].Name, sqlJoinWhere[4], SqlJoinType[4])
                        .OrderBy(orderby, orderType == 0 ? OrderByType.Asc : OrderByType.Desc)
                        .Select<T7>(resFields).ToPageList(page, limit, ref rowCount).ToList();
                }
                else
                {
                    res = _baseDb.Queryable<T>(joinExpression.Parameters[0].Name).AS(typeof(T).Name)
                        .AddJoinInfo(typeof(T2).Name, joinExpression.Parameters[1].Name, sqlJoinWhere[0], SqlJoinType[0])
                        .AddJoinInfo(typeof(T3).Name, joinExpression.Parameters[2].Name, sqlJoinWhere[1], SqlJoinType[1])
                        .AddJoinInfo(typeof(T4).Name, joinExpression.Parameters[3].Name, sqlJoinWhere[2], SqlJoinType[2])
                        .AddJoinInfo(typeof(T5).Name, joinExpression.Parameters[4].Name, sqlJoinWhere[3], SqlJoinType[3])
                        .AddJoinInfo(typeof(T6).Name, joinExpression.Parameters[5].Name, sqlJoinWhere[4], SqlJoinType[4])
                        .Where(where)
                        .OrderBy(orderby, orderType == 0 ? OrderByType.Asc : OrderByType.Desc)
                        .Select<T7>(resFields).ToPageList(page, limit, ref rowCount).ToList();
                }
                return new Result<List<T7>>() { IsSucceed = res != null, Data = res };
            }
            catch (Exception ex)
            {
                return new Result<List<T7>>() { ErrorCode = 500, Message = ex.Message };
            }
        }

        #endregion

        #region 关联查询 扩展
        /// <summary>
        /// 2张表 关联查询
        /// </summary>
        /// <typeparam name="T">第一张表</typeparam>
        /// <typeparam name="T2">第二张表</typeparam>
        /// <typeparam name="T3">返回的数据视图</typeparam>
        /// <param name="joinExpression">关联名</param>
        /// <param name="sqlJoinWhere">格式 a.kid=b.kid and 1=1 </param>
        /// <param name="joinTypes">枚举类型</param>
        /// <param name="where">where条件 Lambda 表达式</param>
        /// <param name="orderby">排序字段 Lambda 表达式</param>
        /// <param name="orderType">排序类型 0ASC 1DESC</param>
        /// <param name="rowCount">总数</param>
        /// <param name="resFields">返回字段列表 a.*,b.*,a.kid as aakid </param>
        /// <param name="page">分页 默认1</param>
        /// <param name="limit">每页条数 默认20 </param>
        public virtual Result<List<T3>> GetList<T, T2, T3>(Expression<Func<T, T2, bool>> joinExpression, string sqlJoinWhere, IDbService.JoinType joinTypes, Expression<Func<T, T2, bool>> where
            , Expression<Func<T, object>> orderby, OrderType orderType, ref int rowCount, string resFields = "", int page = 1, int limit = 20) where T : class, new()
        {
            try
            {
                var sugarJoinType = SqlSugar.JoinType.Left;
                if (joinTypes == IDbService.JoinType.Left) sugarJoinType = SqlSugar.JoinType.Left;
                if (joinTypes == IDbService.JoinType.Right) sugarJoinType = SqlSugar.JoinType.Right;
                if (joinTypes == IDbService.JoinType.Inner) sugarJoinType = SqlSugar.JoinType.Inner;

                var res = new List<T3>();
                if (string.IsNullOrWhiteSpace(resFields))
                {
                    for (int i = 0; i < joinExpression.Parameters.Count(); i++)
                    {
                        resFields += joinExpression.Parameters[i].Name + ".*,";
                    }
                    resFields = resFields.Trim(',');
                }
                if (where == null)
                {
                    //res = _baseDb.Queryable<T>(joinExpression.Parameters[0].Name).AS(typeof(T).Name)
                    //    .AddJoinInfo(typeof(T2).Name, joinExpression.Parameters[1].Name, sqlJoinWhere, sugarJoinType)
                    //    .OrderBy(orderby, orderType == 0 ? OrderByType.Asc : OrderByType.Desc)
                    //    .Select<T3>().ToPageList(page, limit).ToList();

                    res = _baseDb.Queryable<T>(joinExpression.Parameters[0].Name).AS(typeof(T).Name)
                        .AddJoinInfo(typeof(T2).Name, joinExpression.Parameters[1].Name, sqlJoinWhere, sugarJoinType)
                        .OrderBy(orderby, orderType == 0 ? OrderByType.Asc : OrderByType.Desc)
                        //.Select<T3>().ToPageList(page, limit).ToList();
                        //.Select<T3>(resView).ToPageList(page, limit).ToList();
                        .Select<T3>(resFields).ToPageList(page, limit, ref rowCount).ToList();

                }
                else
                {
                    var x = _baseDb.Queryable<T, T2>(joinExpression);
                    x.QueryBuilder.EasyJoinInfos = null;
                    x.AddJoinInfo(typeof(T2).Name, joinExpression.Parameters[1].Name, sqlJoinWhere, sugarJoinType);
                    x.Where(where);
                    x.OrderBy(orderby, orderType == 0 ? OrderByType.Asc : OrderByType.Desc);
                    res = x.Select<T3>(resFields).ToPageList(page, limit, ref rowCount).ToList();
                }
                return new Result<List<T3>>() { IsSucceed = res != null, Data = res };
            }
            catch (Exception ex)
            {
                return new Result<List<T3>>() { ErrorCode = 500, Message = ex.Message };
            }
        }

        /// <summary>
        /// 3张表 关联查询
        /// </summary>
        /// <typeparam name="T">第一张表</typeparam>
        /// <typeparam name="T2">第二张表</typeparam>
        /// <typeparam name="T3">第三张表</typeparam>
        /// <typeparam name="T4">返回的数据视图</typeparam>
        /// <param name="joinExpression">关联名</param>
        /// <param name="sqlJoinWhere">格式 a.kid=b.kid and 1=1 </param>
        /// <param name="joinTypes">枚举类型</param>
        /// <param name="where">where条件 Lambda 表达式</param>
        /// <param name="orderby">排序字段 Lambda 表达式</param>
        /// <param name="orderType">排序类型 0ASC 1DESC</param>
        /// <param name="rowCount">总数</param>
        /// <param name="resFields">返回字段列表 a.*,b.*,a.kid as aakid </param>
        /// <param name="page">分页 默认1</param>
        /// <param name="limit">每页条数 默认20 </param>
        /// <returns></returns>
        public virtual Result<List<T4>> GetList<T, T2, T3, T4>(Expression<Func<T, T2, T3, bool>> joinExpression, List<string> sqlJoinWhere, List<IDbService.JoinType> joinTypes, Expression<Func<T, T2, T3, bool>> where
            , Expression<Func<T, object>> orderby, OrderType orderType, ref int rowCount, string resFields = "", int page = 1, int limit = 20) where T : class, new()
        {
            try
            {
                List<SqlSugar.JoinType> SqlJoinType = new List<SqlSugar.JoinType>();
                joinTypes.ForEach((item) =>
                {
                    if (item == IDbService.JoinType.Left) SqlJoinType.Add(SqlSugar.JoinType.Left);
                    if (item == IDbService.JoinType.Right) SqlJoinType.Add(SqlSugar.JoinType.Right);
                    if (item == IDbService.JoinType.Inner) SqlJoinType.Add(SqlSugar.JoinType.Inner);
                });

                var res = new List<T4>();
                if (string.IsNullOrWhiteSpace(resFields))
                {
                    for (int i = 0; i < joinExpression.Parameters.Count(); i++)
                    {
                        resFields += joinExpression.Parameters[i].Name + ".*,";
                    }
                    resFields = resFields.Trim(',');
                }
                if (where == null)
                {
                    res = _baseDb.Queryable<T>(joinExpression.Parameters[0].Name).AS(typeof(T).Name)
                        .AddJoinInfo(typeof(T2).Name, joinExpression.Parameters[1].Name, sqlJoinWhere[0], SqlJoinType[0])
                        .AddJoinInfo(typeof(T3).Name, joinExpression.Parameters[2].Name, sqlJoinWhere[1], SqlJoinType[1])
                        .OrderBy(orderby, orderType == 0 ? OrderByType.Asc : OrderByType.Desc)
                        .Select<T4>(resFields).ToPageList(page, limit, ref rowCount).ToList();
                }
                else
                {
                    //res = _baseDb.Queryable<T>(joinExpression.Parameters[0].Name).AS(typeof(T).Name)
                    //    .AddJoinInfo(typeof(T2).Name, joinExpression.Parameters[1].Name, sqlJoinWhere[0], SqlJoinType[0])
                    //    .AddJoinInfo(typeof(T3).Name, joinExpression.Parameters[2].Name, sqlJoinWhere[1], SqlJoinType[1])
                    //    .Where(where)
                    //    .OrderBy(orderby, orderType == 0 ? OrderByType.Asc : OrderByType.Desc)
                    //    .Select<T4>(resFields).ToPageList(page, limit, ref rowCount).ToList();

                    var x = _baseDb.Queryable<T, T2, T3>(joinExpression);
                    x.QueryBuilder.EasyJoinInfos = null;
                    x.AddJoinInfo(typeof(T2).Name, joinExpression.Parameters[1].Name, sqlJoinWhere[0], SqlJoinType[0]);
                    x.AddJoinInfo(typeof(T3).Name, joinExpression.Parameters[2].Name, sqlJoinWhere[1], SqlJoinType[1]);
                    x.Where(where);
                    x.OrderBy(orderby, orderType == 0 ? OrderByType.Asc : OrderByType.Desc);
                    res = x.Select<T4>(resFields).ToPageList(page, limit, ref rowCount).ToList();
                }
                return new Result<List<T4>>() { IsSucceed = res != null, Data = res };
            }
            catch (Exception ex)
            {
                return new Result<List<T4>>() { ErrorCode = 500, Message = ex.Message };
            }
        }

        /// <summary>
        /// 4张表 关联查询
        /// </summary>
        /// <typeparam name="T">第一张表</typeparam>
        /// <typeparam name="T2">第二张表</typeparam>
        /// <typeparam name="T3">第三张表</typeparam>
        /// <typeparam name="T4">第四张表</typeparam>
        /// <typeparam name="T5">返回的数据视图</typeparam>
        /// <param name="joinExpression">关联名</param>
        /// <param name="sqlJoinWhere">格式 a.kid=b.kid and 1=1 </param>
        /// <param name="joinTypes">枚举类型</param>
        /// <param name="where">where条件 Lambda 表达式</param>
        /// <param name="orderby">排序字段 Lambda 表达式</param>
        /// <param name="orderType">排序类型 0ASC 1DESC</param>
        /// <param name="rowCount">总数</param>
        /// <param name="resFields">返回字段列表 a.*,b.*,a.kid as aakid </param>
        /// <param name="page">分页 默认1</param>
        /// <param name="limit">每页条数 默认20 </param>
        /// <returns></returns>
        public virtual Result<List<T5>> GetList<T, T2, T3, T4, T5>(Expression<Func<T, T2, T3, T4, bool>> joinExpression, List<string> sqlJoinWhere, List<IDbService.JoinType> joinTypes, Expression<Func<T, T2, T3, T4, bool>> where
            , Expression<Func<T, object>> orderby, OrderType orderType, ref int rowCount, string resFields = "", int page = 1, int limit = 20) where T : class, new()
        {
            try
            {
                List<SqlSugar.JoinType> SqlJoinType = new List<SqlSugar.JoinType>();
                joinTypes.ForEach((item) =>
                {
                    if (item == IDbService.JoinType.Left) SqlJoinType.Add(SqlSugar.JoinType.Left);
                    if (item == IDbService.JoinType.Right) SqlJoinType.Add(SqlSugar.JoinType.Right);
                    if (item == IDbService.JoinType.Inner) SqlJoinType.Add(SqlSugar.JoinType.Inner);
                });

                var res = new List<T5>();
                if (string.IsNullOrWhiteSpace(resFields))
                {
                    for (int i = 0; i < joinExpression.Parameters.Count(); i++)
                    {
                        resFields += joinExpression.Parameters[i].Name + ".*,";
                    }
                    resFields = resFields.Trim(',');
                }
                if (where == null)
                {
                    res = _baseDb.Queryable<T>(joinExpression.Parameters[0].Name).AS(typeof(T).Name)
                        .AddJoinInfo(typeof(T2).Name, joinExpression.Parameters[1].Name, sqlJoinWhere[0], SqlJoinType[0])
                        .AddJoinInfo(typeof(T3).Name, joinExpression.Parameters[2].Name, sqlJoinWhere[1], SqlJoinType[1])
                        .AddJoinInfo(typeof(T4).Name, joinExpression.Parameters[3].Name, sqlJoinWhere[2], SqlJoinType[2])
                        .OrderBy(orderby, orderType == 0 ? OrderByType.Asc : OrderByType.Desc)
                        .Select<T5>(resFields).ToPageList(page, limit, ref rowCount).ToList();
                }
                else
                {
                    //res = _baseDb.Queryable<T>(joinExpression.Parameters[0].Name).AS(typeof(T).Name)
                    //    .AddJoinInfo(typeof(T2).Name, joinExpression.Parameters[1].Name, sqlJoinWhere[0], SqlJoinType[0])
                    //    .AddJoinInfo(typeof(T3).Name, joinExpression.Parameters[2].Name, sqlJoinWhere[1], SqlJoinType[1])
                    //    .AddJoinInfo(typeof(T4).Name, joinExpression.Parameters[3].Name, sqlJoinWhere[2], SqlJoinType[2])
                    //    .Where(where)
                    //    .OrderBy(orderby, orderType == 0 ? OrderByType.Asc : OrderByType.Desc)
                    //    .Select<T5>(resFields).ToPageList(page, limit, ref rowCount).ToList();

                    var x = _baseDb.Queryable<T, T2, T3, T4>(joinExpression);
                    x.QueryBuilder.EasyJoinInfos = null;
                    x.AddJoinInfo(typeof(T2).Name, joinExpression.Parameters[1].Name, sqlJoinWhere[0], SqlJoinType[0]);
                    x.AddJoinInfo(typeof(T3).Name, joinExpression.Parameters[2].Name, sqlJoinWhere[1], SqlJoinType[1]);
                    x.AddJoinInfo(typeof(T4).Name, joinExpression.Parameters[3].Name, sqlJoinWhere[2], SqlJoinType[1]);
                    x.Where(where);
                    x.OrderBy(orderby, orderType == 0 ? OrderByType.Asc : OrderByType.Desc);
                    res = x.Select<T5>(resFields).ToPageList(page, limit, ref rowCount).ToList();
                }
                return new Result<List<T5>>() { IsSucceed = res != null, Data = res };
            }
            catch (Exception ex)
            {
                return new Result<List<T5>>() { ErrorCode = 500, Message = ex.Message };
            }
        }


        /// <summary>
        /// 5张表 关联查询
        /// </summary>
        /// <typeparam name="T">第一张表</typeparam>
        /// <typeparam name="T2">第二张表</typeparam>
        /// <typeparam name="T3">第三张表</typeparam>
        /// <typeparam name="T4">第四张表</typeparam>
        /// <typeparam name="T5">第五张表</typeparam>
        /// <typeparam name="T6">返回的数据视图</typeparam>
        /// <param name="joinExpression">关联名</param>
        /// <param name="sqlJoinWhere">格式 a.kid=b.kid and 1=1 </param>
        /// <param name="joinTypes">枚举类型</param>
        /// <param name="where">where条件 Lambda 表达式</param>
        /// <param name="orderby">排序字段 Lambda 表达式</param>
        /// <param name="orderType">排序类型 0ASC 1DESC</param>
        /// <param name="rowCount">总数</param>
        /// <param name="resFields">返回字段列表 a.*,b.*,a.kid as aakid </param>
        /// <param name="page">分页 默认1</param>
        /// <param name="limit">每页条数 默认20 </param>
        /// <returns></returns>
        public virtual Result<List<T6>> GetList<T, T2, T3, T4, T5, T6>(Expression<Func<T, T2, T3, T4, T5, bool>> joinExpression, List<string> sqlJoinWhere, List<IDbService.JoinType> joinTypes, Expression<Func<T, T2, T3, T4, T5, bool>> where
            , Expression<Func<T, object>> orderby, OrderType orderType, ref int rowCount, string resFields = "", int page = 1, int limit = 20) where T : class, new()
        {
            try
            {
                List<SqlSugar.JoinType> SqlJoinType = new List<SqlSugar.JoinType>();
                joinTypes.ForEach((item) =>
                {
                    if (item == IDbService.JoinType.Left) SqlJoinType.Add(SqlSugar.JoinType.Left);
                    if (item == IDbService.JoinType.Right) SqlJoinType.Add(SqlSugar.JoinType.Right);
                    if (item == IDbService.JoinType.Inner) SqlJoinType.Add(SqlSugar.JoinType.Inner);
                });

                var res = new List<T6>();
                if (string.IsNullOrWhiteSpace(resFields))
                {
                    for (int i = 0; i < joinExpression.Parameters.Count(); i++)
                    {
                        resFields += joinExpression.Parameters[i].Name + ".*,";
                    }
                    resFields = resFields.Trim(',');
                }
                if (where == null)
                {
                    res = _baseDb.Queryable<T>(joinExpression.Parameters[0].Name).AS(typeof(T).Name)
                        .AddJoinInfo(typeof(T2).Name, joinExpression.Parameters[1].Name, sqlJoinWhere[0], SqlJoinType[0])
                        .AddJoinInfo(typeof(T3).Name, joinExpression.Parameters[2].Name, sqlJoinWhere[1], SqlJoinType[1])
                        .AddJoinInfo(typeof(T4).Name, joinExpression.Parameters[3].Name, sqlJoinWhere[2], SqlJoinType[2])
                        .AddJoinInfo(typeof(T5).Name, joinExpression.Parameters[4].Name, sqlJoinWhere[3], SqlJoinType[3])
                        .OrderBy(orderby, orderType == 0 ? OrderByType.Asc : OrderByType.Desc)
                        .Select<T6>(resFields).ToPageList(page, limit, ref rowCount).ToList();
                }
                else
                {
                    //res = _baseDb.Queryable<T, T2, T3, T4, T5>(joinExpression).AS(typeof(T).Name)
                    //    .AddJoinInfo(typeof(T2).Name, joinExpression.Parameters[1].Name, sqlJoinWhere[0], SqlJoinType[0])
                    //    .AddJoinInfo(typeof(T3).Name, joinExpression.Parameters[2].Name, sqlJoinWhere[1], SqlJoinType[1])
                    //    .AddJoinInfo(typeof(T4).Name, joinExpression.Parameters[3].Name, sqlJoinWhere[2], SqlJoinType[2])
                    //    .AddJoinInfo(typeof(T5).Name, joinExpression.Parameters[4].Name, sqlJoinWhere[3], SqlJoinType[3])
                    //    .Where(where)
                    //    .OrderBy(orderby, orderType == 0 ? OrderByType.Asc : OrderByType.Desc)
                    //    .Select<T6>(resFields).ToPageList(page, limit, ref rowCount).ToList();

                    var x = _baseDb.Queryable<T, T2, T3, T4,T5>(joinExpression);
                    x.QueryBuilder.EasyJoinInfos = null;
                    x.AddJoinInfo(typeof(T2).Name, joinExpression.Parameters[1].Name, sqlJoinWhere[0], SqlJoinType[0]);
                    x.AddJoinInfo(typeof(T3).Name, joinExpression.Parameters[2].Name, sqlJoinWhere[1], SqlJoinType[1]);
                    x.AddJoinInfo(typeof(T4).Name, joinExpression.Parameters[3].Name, sqlJoinWhere[2], SqlJoinType[1]);
                    x.AddJoinInfo(typeof(T5).Name, joinExpression.Parameters[4].Name, sqlJoinWhere[3], SqlJoinType[1]);
                    x.Where(where);
                    x.OrderBy(orderby, orderType == 0 ? OrderByType.Asc : OrderByType.Desc);
                    res = x.Select<T6>(resFields).ToPageList(page, limit, ref rowCount).ToList();
                }
                return new Result<List<T6>>() { IsSucceed = res != null, Data = res };
            }
            catch (Exception ex)
            {
                return new Result<List<T6>>() { ErrorCode = 500, Message = ex.Message };
            }
        }

        /// <summary>
        /// 6张表 关联查询
        /// </summary>
        /// <typeparam name="T">第一张表</typeparam>
        /// <typeparam name="T2">第二张表</typeparam>
        /// <typeparam name="T3">第三张表</typeparam>
        /// <typeparam name="T4">第四张表</typeparam>
        /// <typeparam name="T5">第五张表</typeparam>
        /// <typeparam name="T6">第六张表</typeparam>
        /// <typeparam name="T7">返回的数据视图</typeparam>
        /// <param name="joinExpression">关联名</param>
        /// <param name="sqlJoinWhere">格式 a.kid=b.kid and 1=1 </param>
        /// <param name="joinTypes">枚举类型</param>
        /// <param name="where">where条件 Lambda 表达式</param>
        /// <param name="orderby">排序字段 Lambda 表达式</param>
        /// <param name="orderType">排序类型 0ASC 1DESC</param>
        /// <param name="rowCount">数据条数</param>
        /// <param name="resFields">返回字段列表 a.*,b.*,a.kid as aakid </param>
        /// <param name="page">分页 默认1</param>
        /// <param name="limit">每页条数 默认20 </param>
        /// <returns></returns>
        public virtual Result<List<T7>> GetList<T, T2, T3, T4, T5, T6, T7>(Expression<Func<T, T2, T3, T4, T5, T6, bool>> joinExpression, List<string> sqlJoinWhere, List<IDbService.JoinType> joinTypes, Expression<Func<T, T2, T3, T4, T5, T6, bool>> where
            , Expression<Func<T, object>> orderby, OrderType orderType, ref int rowCount, string resFields = "", int page = 1, int limit = 20) where T : class, new()
        {
            try
            {
                List<SqlSugar.JoinType> SqlJoinType = new List<SqlSugar.JoinType>();
                joinTypes.ForEach((item) =>
                {
                    if (item == IDbService.JoinType.Left) SqlJoinType.Add(SqlSugar.JoinType.Left);
                    if (item == IDbService.JoinType.Right) SqlJoinType.Add(SqlSugar.JoinType.Right);
                    if (item == IDbService.JoinType.Inner) SqlJoinType.Add(SqlSugar.JoinType.Inner);
                });

                var res = new List<T7>();
                if (string.IsNullOrWhiteSpace(resFields))
                {
                    for (int i = 0; i < joinExpression.Parameters.Count(); i++)
                    {
                        resFields += joinExpression.Parameters[i].Name + ".*,";
                    }
                    resFields = resFields.Trim(',');
                }
                if (where == null)
                {
                    res = _baseDb.Queryable<T>(joinExpression.Parameters[0].Name).AS(typeof(T).Name)
                        .AddJoinInfo(typeof(T2).Name, joinExpression.Parameters[1].Name, sqlJoinWhere[0], SqlJoinType[0])
                        .AddJoinInfo(typeof(T3).Name, joinExpression.Parameters[2].Name, sqlJoinWhere[1], SqlJoinType[1])
                        .AddJoinInfo(typeof(T4).Name, joinExpression.Parameters[3].Name, sqlJoinWhere[2], SqlJoinType[2])
                        .AddJoinInfo(typeof(T5).Name, joinExpression.Parameters[4].Name, sqlJoinWhere[3], SqlJoinType[3])
                        .AddJoinInfo(typeof(T6).Name, joinExpression.Parameters[5].Name, sqlJoinWhere[4], SqlJoinType[4])
                        .OrderBy(orderby, orderType == 0 ? OrderByType.Asc : OrderByType.Desc)
                        .Select<T7>(resFields).ToPageList(page, limit, ref rowCount).ToList();
                }
                else
                {
                    //res = _baseDb.Queryable<T>(joinExpression.Parameters[0].Name).AS(typeof(T).Name)
                    //    .AddJoinInfo(typeof(T2).Name, joinExpression.Parameters[1].Name, sqlJoinWhere[0], SqlJoinType[0])
                    //    .AddJoinInfo(typeof(T3).Name, joinExpression.Parameters[2].Name, sqlJoinWhere[1], SqlJoinType[1])
                    //    .AddJoinInfo(typeof(T4).Name, joinExpression.Parameters[3].Name, sqlJoinWhere[2], SqlJoinType[2])
                    //    .AddJoinInfo(typeof(T5).Name, joinExpression.Parameters[4].Name, sqlJoinWhere[3], SqlJoinType[3])
                    //    .AddJoinInfo(typeof(T6).Name, joinExpression.Parameters[5].Name, sqlJoinWhere[4], SqlJoinType[4])
                    //    .Where(where)
                    //    .OrderBy(orderby, orderType == 0 ? OrderByType.Asc : OrderByType.Desc)
                    //    .Select<T7>(resFields).ToPageList(page, limit, ref rowCount).ToList();

                    var x = _baseDb.Queryable<T, T2, T3, T4, T5,T6>(joinExpression);
                    x.QueryBuilder.EasyJoinInfos = null;
                    x.AddJoinInfo(typeof(T2).Name, joinExpression.Parameters[1].Name, sqlJoinWhere[0], SqlJoinType[0]);
                    x.AddJoinInfo(typeof(T3).Name, joinExpression.Parameters[2].Name, sqlJoinWhere[1], SqlJoinType[1]);
                    x.AddJoinInfo(typeof(T4).Name, joinExpression.Parameters[3].Name, sqlJoinWhere[2], SqlJoinType[1]);
                    x.AddJoinInfo(typeof(T5).Name, joinExpression.Parameters[4].Name, sqlJoinWhere[3], SqlJoinType[1]);
                    x.AddJoinInfo(typeof(T6).Name, joinExpression.Parameters[5].Name, sqlJoinWhere[4], SqlJoinType[1]);
                    x.Where(where);
                    x.OrderBy(orderby, orderType == 0 ? OrderByType.Asc : OrderByType.Desc);
                    res = x.Select<T7>(resFields).ToPageList(page, limit, ref rowCount).ToList();
                }
                return new Result<List<T7>>() { IsSucceed = res != null, Data = res };
            }
            catch (Exception ex)
            {
                return new Result<List<T7>>() { ErrorCode = 500, Message = ex.Message };
            }
        }

        #endregion
    }
}
